package com.itheima.exer.exercise.ex0925;

import java.util.Scanner;

public class ExceptionDemo {
    public static void main(String[] args) {
        Student student = new Student();
        System.out.println("请输入名字");
        String name = new Scanner(System.in).nextLine();
        student.setName(name);

        while (true) {
            System.out.println("请输入年龄");
            int age = new Scanner(System.in).nextInt();
            try {
                student.setAge(age);
                break;
            } catch (AgeException e) {
                String s = e.toString();
                System.out.println(s);
                continue;
            }
        }
        System.out.println(student);
    }
}

class AgeException extends RuntimeException {
    public AgeException(String message) {
        super(message);
    }

    public AgeException() {
    }
}

class Student {
    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }

    private String name;
    private int age;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        if (age<0 || age > 120) {
            throw new AgeException("年龄不能小于零,不能大于120岁");
        } else {
            this.age = age;

        }
    }

    public Student() {
    }

    public Student(String name, int age) {
        this.name = name;
        this.age = age;
    }
}
